package a66weding.com.jiehuntong.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.adapter.CategoryAdapter;
import a66weding.com.jiehuntong.common.utils.SharesUtils;
import a66weding.com.jiehuntong.common.utils.Utils;
import a66weding.com.jiehuntong.fragment.category.BusinessFragment;
import a66weding.com.jiehuntong.fragment.category.CaseFragment;
import a66weding.com.jiehuntong.fragment.category.CommodityFragment;
import a66weding.com.jiehuntong.fragment.category.PackageFragment;
import a66weding.com.jiehuntong.inner.FragmentListener;
import a66weding.com.jiehuntong.view.EditTextWithAutoClear;
import a66weding.com.jiehuntong.wx.Util;

/**
 * Created by admin on 2016/8/1.
 * 搜索
 */
public class CategorySearchActivity extends FragmentActivity {
    private TabLayout tabLayout;
    private ViewPager vp_newpost;
    private List<String> list;
    private List<FragmentListener> mFragmentListeners;
    private EditTextWithAutoClear editTextWithAutoClear;
    private CategoryAdapter adapter;
    private String searchinfo;
    private String mReturn = "返回";
    private TextView mReturnTv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_category_search);
        mReturnTv = (TextView) findViewById(R.id.cate_title);
        Intent intent = getIntent();
        if (intent != null) {
            searchinfo = intent.getStringExtra("searchinfo");
            mReturn = intent.getStringExtra("return");
            mReturnTv.setText(mReturn);
            SharesUtils.getInstance(this).writeXML("search", searchinfo);
        }
        initContentView();
        editTextWithAutoClear.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }
            @Override
            public void afterTextChanged(Editable editable) {
                SharesUtils.getInstance(CategorySearchActivity.this).writeXML("search", editTextWithAutoClear.getText().toString());
            }
        });

        editTextWithAutoClear.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    String content = editTextWithAutoClear.getText().toString();
                    // 先隐藏键盘
                    ((InputMethodManager) editTextWithAutoClear.getContext().getSystemService(Context.INPUT_METHOD_SERVICE))
                            .hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                    SharesUtils.getInstance(CategorySearchActivity.this).writeXML("search", content);
                    for (FragmentListener f : mFragmentListeners) {
                        f.change(content);
                    }
                    tabLayout.getSelectedTabPosition();
                    return true;
                }
                return false;
            }
        });
    }
    private void initContentView() {
        tabLayout = (TabLayout) findViewById(R.id.tab_essence);
        vp_newpost = (ViewPager) findViewById(R.id.vp_essence);
        String[] titles = getResources().getStringArray(R.array.category_search_tab);

        adapter = new CategoryAdapter(getSupportFragmentManager(), Arrays.asList(titles));
        this.vp_newpost.setAdapter(adapter);
        this.tabLayout.setupWithViewPager(this.vp_newpost);
        vp_newpost.setCurrentItem(0);
        editTextWithAutoClear = (EditTextWithAutoClear) findViewById(R.id.cate_search);
        if (!TextUtils.isEmpty(searchinfo)) {
            editTextWithAutoClear.setText(searchinfo);
            SharesUtils.getInstance(this).writeXML("search", searchinfo);
        }
    }

    public void setListener(FragmentListener listener) {
        if (listener != null) {
            if (mFragmentListeners == null) {
                mFragmentListeners = new ArrayList<>();
            }
            mFragmentListeners.add(listener);
        }
    }

    public void removeListener(FragmentListener listener) {
        if (listener != null) {
            if (mFragmentListeners != null && mFragmentListeners.size() > 0) {
                mFragmentListeners.remove(listener);
            }
        }
    }

    public void back(View view) {
        finish();
    }
}
